package com.yunduansing.demo.web.system;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import com.yunduansing.demo.entity.DictionaryCategoryEntity;
import com.yunduansing.demo.entity.DictionaryEntity;
import com.yunduansing.demo.exception.RecordExistsException;
import com.yunduansing.demo.exception.ServiceException;
import com.yunduansing.demo.model.common.PageRequestModel;
import com.yunduansing.demo.model.common.PageResultModel;
import com.yunduansing.demo.model.common.ResponseModel;
import com.yunduansing.demo.model.dictionary.DictionaryCategoryTreeModel;
import com.yunduansing.demo.model.dictionary.DictionaryPageRequestModel;
import com.yunduansing.demo.model.dictionary.DictionaryTreeModel;
import com.yunduansing.demo.service.DictionaryCategoryService;
import com.yunduansing.demo.service.DictionaryService;
import com.yunduansing.demo.web.BaseController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = "字典相关")
@RestController
public class DictionaryController extends BaseController {
    @Autowired
    private DictionaryService service;
    @Autowired
    private DictionaryCategoryService categoryService;

    @GetMapping("/dictionary/page")
    @ApiOperation("字典分页")
    public ResponseModel<PageResultModel<DictionaryEntity>> getPage(PageRequestModel model) {
        return ok(service.getPage(model));
    }

    @PostMapping("/dictionary/create")
    @ApiOperation("创建字典")
    public ResponseModel<Boolean> create(@RequestBody DictionaryEntity model) throws RecordExistsException {
        return convertToResponseModel(service.create(model), "创建失败。");
    }

    @PostMapping("/dictionary/update")
    @ApiOperation("修改字典")
    public ResponseModel update(@RequestBody DictionaryEntity model) throws RecordExistsException {
        return convertToResponseModel(service.updateById(model), "修改失败。");
    }

    @PostMapping("/dictionary/delete")
    @ApiOperation("删除字典")
    public ResponseModel<Boolean> delete(@RequestBody List<Integer> ids) {
        return convertToResponseModel(service.removeByIds(ids), "删除失败。");
    }

    @GetMapping("/dictionary/tree")
    @ApiOperation("字典树形结构")
    public ResponseModel<List<DictionaryTreeModel>> getTree(DictionaryPageRequestModel model) {
        return ok(service.getTree(model));
    }

    @PostMapping("/dictionary/category/create")
    @ApiOperation("创建类型")
    public ResponseModel<Boolean> createCategory(@RequestBody DictionaryCategoryEntity model) throws RecordExistsException {
        return convertToResponseModel(categoryService.create(model), "创建失败。");
    }

    @PostMapping("/dictionary/category/update")
    @ApiOperation("修改类型")
    public ResponseModel<Boolean> updateCategory(@RequestBody DictionaryCategoryEntity model) throws RecordExistsException {
        return convertToResponseModel(categoryService.update(model), "修改失败。");
    }

    @PostMapping("/dictionary/category/delete")
    @ApiOperation("删除类型")
    public ResponseModel<Boolean> deleteCategory(@RequestBody List<Integer> ids) throws ServiceException {
        return convertToResponseModel(categoryService.delete(ids) ,"删除失败。");
    }

    @GetMapping("/dictionary/category/tree")
    @ApiOperation("类型树形结构")
    public ResponseModel<List<DictionaryCategoryTreeModel>> getCategoryTree(){
        return ok(categoryService.getTree());
    }

    @GetMapping("/dictionary/category/code")
    @ApiOperation("类型树形结构")
    public ResponseModel<List<DictionaryTreeModel>> getByCategoryCode(String categoryCode){
        var data=service.getAll().stream().filter(x->x.getCategoryCode().equals(categoryCode)).collect(Collectors.toList());
        return ok(data);
    }
}